<?php
namespace app\dao;

use app\dao\interfaces\IAppInstallLoglDao;
use common\dao\CommonDao;
use herosphp\bean\Beans;
use herosphp\core\Loader;
use herosphp\utils\ArrayUtils;

Loader::import('app.dao.interfaces.IAppInstallLoglDao');

/**
 * app(DAO)接口实现
 * @package app\dao
 * @author yangjian<yangjian102621@gmail.com>
 */
class AppInstallLoglDao extends CommonDao implements IAppInstallLoglDao {

    public function getItem($conditions, $fields, $order)
    {
        $item = parent::getItem($conditions, $fields, $order);
        if ( !empty($item) ) {
            $model = Loader::model("Company");
            $company = $model->getItem(array("userid" => $item['userid']));
            if ( !empty($company) ) {
                $item = array_merge($company, $item);
            }
        }

        return $item;
    }

    public function count($conditions)
    {
        if ( isset($conditions['username']) || isset($conditions['name']) ) { //判断是否是通过会员字段搜索
            $userService = Beans::get('user.user.service');
            $items = $userService->getItems($conditions, 'id');
            if ( !empty($items) ) {
                $ids = array();
                foreach ( $items as $val ) {
                    $ids[] = $val['id'];
                }
                $conditions = array('userid' => array('$in' => $ids));
            } else {
                return false;
            }
        }
        return parent::count($conditions); // TODO: Change the autogenerated stub
    }

    public function getItems($conditions, $fields, $order, $limit, $group, $having)
    {

        if ( isset($conditions['username']) || isset($conditions['name']) ) { //判断是否是通过会员字段搜索
            $userService = Beans::get('user.user.service');
            $items = $userService->getItems($conditions, 'id');
            if ( !empty($items) ) {
                $ids = array();
                foreach ( $items as $val ) {
                    $ids[] = $val['id'];
                }
                $conditions = array('userid' => array('$in' => $ids));
            } else {
                return false;
            }
        }
        $items = parent::getItems($conditions, $fields, $order, $limit, $group, $having);
        if ( !empty($items) ) {
            $ids = array();
            foreach ( $items as $val ) {
                $ids[] = $val['userid'];
            }
            $model = Loader::model("company");
            $userModel = Loader::model('user');
            $companys = $model->getItems(array('userid' => array('$in' => $ids)));
            $users = $userModel->getItems(array('id' => array('$in' => $ids)));
            $companys = ArrayUtils::changeArrayKey($companys, 'userid');
            $users = ArrayUtils::changeArrayKey($users, 'id');
            foreach ( $items as $key => $value ) {
                if ( !empty($users[$value['userid']]) ) {
                    $items[$key]['company'] = $companys[$value['userid']]['company'];
                    $items[$key]['name'] = $users[$value['userid']]['name'];
                    $items[$key]['mobile'] = $users[$value['userid']]['username'];
                }
            }
        }

        return $items;
    }

}
